/*************************************************************************** 
 * (c) 2006 Dmitry Osinovsky, osdmsoftware@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 ***************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace OsdmSoftware.CommonLibrary
{
    public partial class ObjectEditor: UserControl
    {
        Type propType;
        object currentType;
        object obj;
        public object value;
        PropertyEditors pe;
        const String NullString = "Null";
        public ObjectEditor(PropertyEditors pe, string propertyName, string propertyLabel, Type propType, object obj, object value)
        {
            InitializeComponent();
            this.label1.Text = propertyLabel;
            this.propType = propType;
            List<Type> types = TypeTree.theTree.GetDerivedTypes(propType);
            comboBox1.Items.Add(NullString);
            foreach (Type t in types)
                comboBox1.Items.Add(t);
            if (value == null)
            {
                comboBox1.SelectedIndex = 0;
                currentType = NullString;
            }
            else
            {
                comboBox1.SelectedIndex = comboBox1.Items.IndexOf(value.GetType());
                currentType = value.GetType();
            }
            this.obj = obj;
            this.value = value;
            this.pe = pe;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (currentType != comboBox1.SelectedItem)
            {
                if (comboBox1.SelectedItem == (object) NullString)
                {
                    value = null;
                    currentType = NullString;
                    //obj.GetType().GetProperty(this.label1.Text).SetValue(obj, null, null);
                }
                else
                {
                    Type t = (Type) comboBox1.SelectedItem;
                    ConstructorInfo c = t.GetConstructor(new Type[0]);
                    value = c.Invoke(null);
                    currentType = value.GetType();
                }
            }

            if (value != null)
                pe.EditObject(value);
        }
    }
}
